package com.letao.server.consumer;

import com.alibaba.fastjson.JSON;
import com.letao.server.dto.RabbitMqSpuInfoDTO;
import com.letao.server.utils.EsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpuConsumer {
    EsUtils esUtils = new EsUtils();

    @RabbitListener(queues = "spu_info")
    private void handleSpuInfo(String spuInfo) {
        RabbitMqSpuInfoDTO rabbitMqSpuInfo = JSON.parseObject(spuInfo, RabbitMqSpuInfoDTO.class);
        if ("insert".equals(rabbitMqSpuInfo.getType()) ||
                ("update".equals(rabbitMqSpuInfo.getType()))
                        && rabbitMqSpuInfo.getData().getBit() == 1) {
            esUtils.insertAndUpdateSpu(rabbitMqSpuInfo.getData());
        }
        if (("update".equals(rabbitMqSpuInfo.getType()) && rabbitMqSpuInfo.getData().getBit() == 0)
                || "delete".equals(rabbitMqSpuInfo.getType())) {
            esUtils.deleteSpu(rabbitMqSpuInfo.getData());
        }
    }
}
